/*
 *此javaCode由YMS-JDBC替换工具生成，原mapper位置com.yonyou.ucf.mdf.app.isvdxq.firstpart.appconfig.mapper.AppconfigMapper
 */
package com.yonyou.ucf.mdf.app.isvdxq.firstpart.appconfig.mapper;

import com.yonyou.iuap.yms.api.IYmsJdbcApi;
import com.yonyou.iuap.yms.param.*;
import com.yonyou.iuap.yms.param.ResultSetProcessor;
import com.yonyou.iuap.yms.processor.*;
import com.yonyou.iuap.yms.replace.CollectionUtil;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.appconfig.entity.AppconfigEntity;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.appconfig.entity.TabInfoEntity;

import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class AppconfigMapperImpl {
    
    @Autowired
    private IYmsJdbcApi ymsJdbcApi;

    public List<AppconfigEntity> selectAll(String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "select * from isv_appconfig where  `dr`=0 and ytenant_id=?\n";
        parameter.addParam(tenant_id);

        ResultSetProcessor processor = new AutoBeanListProcessor(new AppconfigEntity());
        return (List<AppconfigEntity>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }


    public List<AppconfigEntity> selectByIds(List<String> strList) {
		if (strList==null || strList.size()==0) return new ArrayList<>();
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" select * from isv_appconfig where dr=0 ");
		if (CollectionUtil.isNotEmpty(strList)) {
			sqlBuilder.append(" and id in ");
			sqlBuilder.append(" ( ");
			String separator0 = " , ";
			for (int index = 0; index < strList.size(); index++) {
				String item = strList.get(index);
				sqlBuilder.append(" ? ");
				parameter.addParam(item);
				if (index < strList.size() - 1) {
					sqlBuilder.append(separator0);
				}
			}
			sqlBuilder.append(" ) ");
		}

        
        String sql = sqlBuilder.toString();
        ResultSetProcessor processor = new AutoBeanListProcessor(new AppconfigEntity());
        return (List<AppconfigEntity>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }


    public AppconfigEntity SelectLastPrintConfig(String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "select * from isv_appconfig where appType = 2 and status = 1 and  dr=0 and ytenant_id=? order by create_time desc limit 1\n";
        parameter.addParam(tenant_id);

        ResultSetProcessor processor = new AutoBeanProcessor(new AppconfigEntity());
        return (AppconfigEntity) ymsJdbcApi.queryForObject(sql, parameter, processor);
    }


    public int updateByPrimaryKey(AppconfigEntity record) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "update isv_appconfig set `ytenant_id` = ?, `appType` = ?, `appCode` = ?, `applicationId` = ?, `localservurl` = ?, `status` = ?, `dr` = ?, `create_time` = ?, `creator` = ?, `creatorName` = ?, `modify_time` = ?, `modifier` = ?, `modifierName` = ?, `pubts` = ?, `version` = ? where `id` = ?\n";
        parameter.addParamWithType(record.getTenant_id(), Types.BIGINT);
		parameter.addParamWithType(record.getVersion(), Types.INTEGER);
		parameter.addParamWithType(record.getAppcode(), Types.VARCHAR);
		parameter.addParamWithType(record.getApplicationid(), Types.VARCHAR);
		parameter.addParamWithType(record.getLocalservurl(), Types.VARCHAR);
		parameter.addParamWithType(record.getStatus(), Types.INTEGER);
		parameter.addParamWithType(record.getDr(), Types.SMALLINT);
		parameter.addParamWithType(record.getCreateTime(), Types.VARCHAR);
		parameter.addParamWithType(record.getCreator(), Types.VARCHAR);
		parameter.addParamWithType(record.getCreatorname(), Types.VARCHAR);
		parameter.addParamWithType(record.getModifyTime(), Types.VARCHAR);
		parameter.addParamWithType(record.getModifier(), Types.VARCHAR);
		parameter.addParamWithType(record.getModifiername(), Types.VARCHAR);
		parameter.addParamWithType(record.getPubts(), Types.TIMESTAMP);
		parameter.addParamWithType(record.getVersion(), Types.INTEGER);
		parameter.addParamWithType(record.getId(), Types.BIGINT);

        
        return ymsJdbcApi.update(sql, parameter);
    }


    public int selectCount(Long id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "select count(1) from isv_appconfig where `id`=? and dr=0;\n";
        parameter.addParam(id);

        ResultSetProcessor processor = new ColumnProcessor(int.class);
        return (int) ymsJdbcApi.queryForObject(sql, parameter, processor);
    }


    public int insert(AppconfigEntity record) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "insert into isv_appconfig (`id`, `ytenant_id`, `appCode`, `applicationId`, `localservurl`, `status`, `dr`,  `create_time`, `modify_time`,  `creator`, `creatorName`,  `modifier`, `modifierName`,  `pubts`, `version`,`appType`) values (?, ?, ?, ?, ?,  ?, ?,  ?, ?,  ?, ?,  ?, ?,  ?, ?,?)\n";
        parameter.addParamWithType(record.getId(), Types.BIGINT);
		parameter.addParamWithType(record.getTenant_id(), Types.BIGINT);
		parameter.addParamWithType(record.getAppcode(), Types.VARCHAR);
		parameter.addParamWithType(record.getApplicationid(), Types.VARCHAR);
		parameter.addParamWithType(record.getLocalservurl(), Types.VARCHAR);
		parameter.addParamWithType(record.getStatus(), Types.INTEGER);
		parameter.addParamWithType(record.getDr(), Types.SMALLINT);
		parameter.addParamWithType(record.getCreateTime(), Types.VARCHAR);
		parameter.addParamWithType(record.getModifyTime(), Types.VARCHAR);
		parameter.addParamWithType(record.getCreator(), Types.VARCHAR);
		parameter.addParamWithType(record.getCreatorname(), Types.VARCHAR);
		parameter.addParamWithType(record.getModifier(), Types.VARCHAR);
		parameter.addParamWithType(record.getModifiername(), Types.VARCHAR);
		parameter.addParamWithType(record.getPubts(), Types.TIMESTAMP);
		parameter.addParamWithType(record.getVersion(), Types.INTEGER);
		parameter.addParamWithType(record.getAppType(), Types.INTEGER);

        
        return ymsJdbcApi.update(sql, parameter);
    }


    public int insertBatch(List<AppconfigEntity> insertBatch) {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" insert into isv_appconfig (`id`, `ytenant_id`, `appType`,`appCode`, `applicationId`, `localservurl`, `status`, `dr`, `create_time`, `modify_time`, `creator`, `creatorName`, `modifier`, `modifierName`, `pubts`, `version`) values ");
		if (CollectionUtil.isNotEmpty(insertBatch)) {
			String separator0 = " , ";
			for (int i = 0; i < insertBatch.size(); i++) {
				AppconfigEntity item = insertBatch.get(i);
				sqlBuilder.append(" (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
				parameter.addParamWithType(item.getId(), Types.BIGINT);
				parameter.addParamWithType(item.getTenant_id(), Types.BIGINT);
				parameter.addParamWithType(item.getAppType(), Types.INTEGER);
				parameter.addParamWithType(item.getAppcode(), Types.VARCHAR);
				parameter.addParamWithType(item.getApplicationid(), Types.VARCHAR);
				parameter.addParamWithType(item.getLocalservurl(), Types.VARCHAR);
				parameter.addParamWithType(item.getStatus(), Types.INTEGER);
				parameter.addParamWithType(item.getDr(), Types.SMALLINT);
				parameter.addParamWithType(item.getCreateTime(), Types.VARCHAR);
				parameter.addParamWithType(item.getModifyTime(), Types.VARCHAR);
				parameter.addParamWithType(item.getCreator(), Types.VARCHAR);
				parameter.addParamWithType(item.getCreatorname(), Types.VARCHAR);
				parameter.addParamWithType(item.getModifier(), Types.VARCHAR);
				parameter.addParamWithType(item.getModifiername(), Types.VARCHAR);
				parameter.addParamWithType(item.getPubts(), Types.TIMESTAMP);
				parameter.addParamWithType(item.getVersion(), Types.INTEGER);
				if (i < insertBatch.size() - 1) {
					sqlBuilder.append(separator0);
				}
			}
		}
		sqlBuilder.append(" ON DUPLICATE KEY UPDATE appcode=VALUES(appcode),applicationid=VALUES(applicationid),localservurl=VALUES(localservurl) ,modifiername=VALUES(modifiername),modifier=VALUES(modifier) ");

        
        String sql = sqlBuilder.toString();
        
        return ymsJdbcApi.update(sql, parameter);
    }


    public int deleteByPrimaryKeys(List<Long> ids) {
		if (ids == null || ids.size()==0) return 0;
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" update isv_appconfig set `dr` = 1 where `id` in ( ");
		if (CollectionUtil.isNotEmpty(ids)) {
			String separator0 = " , ";
			for (int i = 0; i < ids.size(); i++) {
				Long item = ids.get(i);
				sqlBuilder.append(" ? ");
				parameter.addParamWithType(item, Types.BIGINT);
				if (i < ids.size() - 1) {
					sqlBuilder.append(separator0);
				}
			}
		} else {
			sqlBuilder.append(" '' ");
		}
		sqlBuilder.append(" ) ");

        
        String sql = sqlBuilder.toString();
        
        return ymsJdbcApi.update(sql, parameter);
    }


    public int deleteByPrimaryKey(Long id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "delete from isv_appconfig where `id` = ?\n";
        parameter.addParamWithType(id, Types.BIGINT);

        
        return ymsJdbcApi.update(sql, parameter);
    }


    public AppconfigEntity selectById(String id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "select * from isv_appconfig where `id`=?;\n";
        parameter.addParam(id);

        ResultSetProcessor processor = new AutoBeanProcessor(new AppconfigEntity());
        return (AppconfigEntity) ymsJdbcApi.queryForObject(sql, parameter, processor);
    }


    public List<TabInfoEntity> selectTabInfoById(String id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "select table_name tableName,engine, table_comment tableComment, table_collation tableCollation, create_time createTime  from information_schema.tables  where table_schema = ?\n";
        parameter.addParam(id);

        ResultSetProcessor processor = new AutoBeanListProcessor(new TabInfoEntity());
        return (List<TabInfoEntity>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }


    public AppconfigEntity SelectLastEntity(String tenant_id, String appCode, Integer appType) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "select * from isv_appconfig   where appType = ? and appCode = ?  and ytenant_id=?  order by create_time desc limit 1\n";
        parameter.addParam(appType);
		parameter.addParam(appCode);
		parameter.addParam(tenant_id);

        ResultSetProcessor processor = new AutoBeanProcessor(new AppconfigEntity());
        return (AppconfigEntity) ymsJdbcApi.queryForObject(sql, parameter, processor);
    }

}
